/***************************************************************************
 *   Copyright (C) 2006 by Massimiliano Torromeo                           *
 *   massimiliano.torromeo@gmail.com                                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <unistd.h>
#include <iostream>

#include "../include/touchscreen.h"

using namespace std;

Touchscreen::Touchscreen() {
    wm97xx = 0;
    calibrated = false;
    wasPressed = false;
    x = 0;
    y = 0;
}

Touchscreen::~Touchscreen() {
    if (wm97xx) deinit();
}

bool Touchscreen::init() {
#if defined( __arm__ ) && !defined(_CAANOO_)
    wm97xx = open("/dev/touchscreen/wm97xx", O_RDONLY|O_NOCTTY);//wiz doesn't have /dev/touchscreen/wm97xx
    return wm97xx>0;
#else
    return true;
#endif
}

void Touchscreen::deinit() {
#if defined( __arm__ ) && !defined(_CAANOO_)
    close(wm97xx);
#endif
}

void Touchscreen::calibrate() {
    if (event.pressure==0) {
        calibX = ((event.x-200)*320/3750)/4;
        calibY = (((event.y-200)*240/3750))/4;
        calibrated = true;
    }
}

bool Touchscreen::poll() {
    wasPressed = pressed();
#if defined( _CAANOO_)
    //Caanoo's touchscreen is processed in poll_event (termula2x.c)
    return false;
#elif defined( __arm__ )
    read(wm97xx, &event, sizeof(TS_EVENT));
    if (!calibrated) calibrate();

    if (event.pressure>0) {
        x = ((event.x-200)*320/3750);//-calibX;
        y = (240 - ((event.y-200)*240/3750));//-calibY;
    }
    return pressed();
#else
    SDL_PumpEvents();
    int mx, my;
    if (SDL_GetMouseState(&mx,&my) && SDL_BUTTON(1)) {
        printf("touchdown! at:");
        x = mx;
        y = my;
        event.pressure = 1;
        printf("x=%d, y=%d \n",x,y);
    } else {
        event.pressure = 0;
    }
    return pressed();
#endif
}

bool Touchscreen::pressed() {
    return event.pressure>0;
}

bool Touchscreen::inRect(SDL_Rect r) {
    return (y>=r.y) && (y<=r.y+r.h) && (x>=r.x) && (x<=r.x+r.w);
}

bool Touchscreen::inRect(int x, int y, int w, int h) {
    SDL_Rect rect = {x,y,w,h};
    return inRect(rect);
}
